package org.jeecg.modules.syncdata.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.ApiSignUtil;
import org.jeecg.modules.syncdata.entity.SyncResult;
import org.jeecg.modules.syncdata.mapper.SyncResultMapper;
import org.jeecg.modules.syncdata.service.SyncResultService;
import org.jeecg.modules.syncdata.service.SyncService;
import org.jeecg.modules.thirdpartyapi.constant.BgApiEnum;
import org.jeecg.modules.thirdpartyapi.dto.SyncLogDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * 同步结果服务实现类
 *
 * @author zhanglei01
 * @date 2022/03/23
 */
@Slf4j
@Service
public class SyncResultServiceImpl extends ServiceImpl<SyncResultMapper, SyncResult>
        implements SyncResultService {

    @Autowired
    private SyncService syncService;

    private static final String API_NAME = "getSyncLog";

    @Override
    public String getNextTime(String tableName, String firstTime) {
        SyncResult byId = getById(tableName);
        if (StringUtils.isBlank(firstTime) && byId == null) {
            firstTime = DateUtil.yesterday().toDateStr();
        }
        if (byId == null) {
            SyncResult syncResult = new SyncResult();
            syncResult.setSyncTable(tableName);
            syncResult.setFirstTime(firstTime);
            boolean save = save(syncResult);
            if (!save) {
                throw new JeecgBootException("创建表数据失败!");
            }
        }
        return StringUtils.isBlank(firstTime) ? byId.getNextTime() : firstTime;
    }

    @Override
    public Boolean setNextTime(String tableName, String nextTime) {
        return setNextTime(tableName, nextTime, null);
    }

    @Override
    public Boolean setNextTime(String tableName, String nextTime, String msg) {
        SyncResult syncResult = new SyncResult();
        syncResult.setSyncTable(tableName);
        syncResult.setNextTime(nextTime);
        syncResult.setMsg(msg);
        boolean b = updateById(syncResult);
        //推送
        try {
            SyncResult byId = getById(tableName);
            syncLogPush("sync_result", byId);
        } catch (Exception e) {
            log.error("推送sync_result异常", e);
        }
        return b;
    }

    @Override
    public Boolean getSyncResultExceptOrder(String dayTime) {
        Integer syncResult = getBaseMapper().getSyncResult(dayTime);
        //去掉两个order表
        int length = BgApiEnum.values().length - 2;
        if (syncResult >= length) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean isAllFinish() {
        String yesterday = DateUtil.yesterday().toDateStr();
        Integer syncResult = getBaseMapper().getSyncResult(yesterday);
        int length = BgApiEnum.values().length;
        if (syncResult >= length) {
            return true;
        }
        return false;
    }

    private void syncLogPush(String logType, Object syncLog) {
        try {
            SyncLogDto syncLogDto = new SyncLogDto();
            syncLogDto.setLogType(logType);
            String gzip = ApiSignUtil.gzip(JSONArray.toJSONString(Arrays.asList(syncLog)));
            syncLogDto.setDataMsg(gzip);
            syncService.callThirdPartyApiUnUpGzip(API_NAME, syncLogDto);
        } catch (Exception e) {
            log.error("调用api:{} 异常", API_NAME, e);
        }
    }

    @Override
    public boolean syncZbjkDataNum() {
        // int count = baseMapper.syncZbjkDataNum().size();
        LambdaUpdateWrapper<SyncResult> wrapper = Wrappers.lambdaUpdate(SyncResult.class);
        wrapper.in(SyncResult::getSyncTable,"zbjk_cwzbjkjg","zbjk_fpzbjkjg","zbjk_sszbjkjg").likeRight(SyncResult::getMsg,"最后同步数");
        int count1 = super.count(wrapper);
        // System.out.println(count1);
        return count1 > 0;
    }
}
